import prisma from '@/server/lib/prisma';
import { Prisma } from '@prisma/client';

export default defineEventHandler(async (event) => {
  try {
    const query = getQuery(event);
    const page = Number(query.page || 1);
    const pageSize = Number(query.pageSize || 10);

    // 查询条件
    const where: Prisma.sys_menuWhereInput = {}
    if (query.menuName) {
      where.menuName = {
        contains: query.menuName as string
      }
    }
    if (query.visible) {
      where.visible = query.visible as string
    }

    const result = await prisma.sys_menu.findMany({
      where,
      skip: (page - 1) * pageSize,
      take: pageSize,
      orderBy: {
        orderNum: 'asc'
      }
    })
    return {
      code: 200,
      data: result,
    }
  } catch (e) {
    throw e;
  } finally {
    await prisma.$disconnect(); // 关闭连接
  }
})